
//判断字符串str是否是纯字符元素
function isAllChar(str){
  var reg = /^[a-z]+$/;
  if(reg.test(str)){
    return true;
  }else{
    return false;
  }
}
//把集合collection中相同的元素统计出数量，存入对象数组arr
function count(collection,arr){
  let obj = {};  
  let regNum = /[^0-9]+/ig;
  let regChar = /[^a-z]+/ig;
  let sum,name;
  for(let item of collection){
    if(isAllChar(item)){
      sum = 1;
      name=item;
    }else{
      sum = parseInt(item.replace(regNum,""));
      name = item.replace(regChar,"");
    }
    if(!obj[name]){
      obj[name] = {};
      obj[name].key = name;
      obj[name].count = sum;
    }else{
      obj[name].count =obj[name].count+sum;
    }
  }
  for(let item in obj){
    arr.push(obj[item]);
  }
}
//判断元素ch是否存在于对象object的value属性中
function includes(object,ch){
  for(let valueItem in object){
    for(let item of object[valueItem])
      if(item === ch){
        return true;
      }
  }
  return false;
}

function create_updated_collection(collection_a, object_b) {
  let result = [];
  count(collection_a,result);
  for(let objItem of result){
    if(includes(object_b,objItem.key)){
      objItem.count=objItem.count-parseInt(objItem.count/3);
    }
  }
  return result;
}

module.exports = create_updated_collection;
